package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.module.admin.entity.SysUserCard;
import com.mars.module.admin.mapper.SysUserCardMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysReviewRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysReviewMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysReview;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysReviewService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 发帖评论业务层处理
 *
 * @author mars
 * @date 2024-03-14
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysReviewServiceImpl implements ISysReviewService {

    private final SysReviewMapper baseMapper;
    private final SysUserCardMapper cardMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysReview add(SysReviewRequest request) {
        SysReview entity = SysReview.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysReviewRequest request) {
        SysReview entity = SysReview.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysReview getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysReview> pageList(SysReviewRequest request) {
        Page<SysReview> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysReview> query = this.buildWrapper(request);
        IPage<SysReview> pageRecord = baseMapper.selectPage(page, query);
        List<SysReview> records = pageRecord.getRecords();
        List<SysReview> recordsNew = new ArrayList<>();
        for (SysReview e : records) {
            SysUser sysUser = userMapper.selectById(e.getUserId());
            e.setUserName(sysUser == null ? "" : sysUser.getUserName());
            recordsNew.add(e);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysReview> list(SysReviewRequest request) {
        LambdaQueryWrapper<SysReview> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysReview> buildWrapper(SysReviewRequest param) {
        LambdaQueryWrapper<SysReview> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysReview::getContent, param.getContent());
        }
        if (param.getUserId() != null) {
            query.like(SysReview::getUserId, param.getUserId());
        }
        if (param.getCardId() != null) {
            query.eq(SysReview::getCardId, param.getCardId());
        }
        return query;
    }

}
